#include "StdAfx.h"
#include "MediaManager.h"

CMediaManager::CMediaManager(void)
{
}

CMediaManager::~CMediaManager(void)
{
}

bool  CMediaManager::Init()
{
	RtpInit();
	ms_init();

	//m_configSound.InitSoundConfig();
	MediaCodecsInit();
	return true;
}

bool CMediaManager::RtpInit()
{
	ortp_init();
	rtp_profile_set_payload(&av_profile,115,&payload_type_lpc1015);
	rtp_profile_set_payload(&av_profile,110,&payload_type_speex_nb);
	rtp_profile_set_payload(&av_profile,111,&payload_type_speex_wb);
	rtp_profile_set_payload(&av_profile,112,&payload_type_ilbc);
	rtp_profile_set_payload(&av_profile,116,&payload_type_truespeech);
	rtp_profile_set_payload(&av_profile,101,&payload_type_telephone_event);

	//rtp_config_read(lc);

	return true;

}

void CMediaManager::MediaCodecsInit()
{
	set<string> setPrependCode;
	setPrependCode.insert("speex");
	setPrependCode.insert("MP4V-ES");
	setPrependCode.insert("H264");

	MSList *audiopt,*videopt;
	PayloadType *pPayload = NULL;
	CPayloadTypePtr spPayload;
	bool bPrepend = false;
	m_pLocalRtpProfile = rtp_profile_clone_full(&av_profile);

	for (int i = 0; i < RTP_PROFILE_MAX_PAYLOADS; i++)
	{
		spPayload.Clone(rtp_profile_get_payload(m_pLocalRtpProfile,i));

		if (NULL != spPayload /*&& payload_type_get_user_data(pPayload) != NULL*/)
		{
			// find a mediastreamer codec for this payload type
			if (ms_filter_codec_supported(spPayload->mime_type))
			{
				MSFilterDesc *desc = ms_filter_get_encoder(spPayload->mime_type);
				Soap_Msg("CMediaManager::MediaCodecsInit() -> Adding new codec %s/%i",spPayload->mime_type,spPayload->clock_rate);
				spPayload.SetEnable();
				spPayload.SetUserData((void *)desc->text);
				// by default, put speex, mpeg4, or h264 on top of list
                bPrepend = setPrependCode.count(string(spPayload->mime_type)) > 0;
			
				if (strcasecmp(spPayload->mime_type,"H264") == 0)
				{
					if (!check_h264_packmode(spPayload,desc))
					{
						continue;
					}
				}

				switch (spPayload->type)
				{
					case PAYLOAD_AUDIO_CONTINUOUS:
					case PAYLOAD_AUDIO_PACKETIZED:
						m_cMediaCodecs.AddAudioCode(spPayload,bPrepend);
						break;
					case PAYLOAD_VIDEO:
						m_cMediaCodecs.AddAudioCode(spPayload,bPrepend);
						break;
					default:
						Soap_Error("CMediaManager::MediaCodecsInit -> Unsupported rtp media type.");
				}
			}
		}
	}

}



bool  CMediaManager::check_h264_packmode(PayloadType *payload, MSFilterDesc *desc)
{
	if (payload->recv_fmtp == NULL || strstr(payload->recv_fmtp,"packetization-mode") == 0)
	{
		/*this is packetization-mode=0 H264, we only support it with a multislicing
		enabled version of x264*/
		if (strstr(desc->text,"x264") && strstr(desc->text,"multislicing") == 0)
		{
			/*this is x264 without multisclicing*/
			Soap_Msg("Disabling packetization-mode=0 H264 codec because "
				"of lack of multislicing support");
			return false;
		}
	}

	return true;
}



